package libnetdb.dm.assoc.dhp.demo;
import java.util.*;
import java.text.*;
import java.io.*;

import libnetdb.dm.assoc.base.*;
import libnetdb.dm.assoc.dhp.*;
import libnetdb.dm.assoc.util.*;

public class DemoApp1 {
    private static ArrayList indexList = new ArrayList();
//    {
//        IndexManager manager = IndexManager.getInstance();
//        Iterator it = manager.builderIterator();
//        while(it.hasNext())
//            indexList.add(((IndexBuilder)it.next()).createIndex());        
//    }

    private static void printUsage(PrintStream ps){
        ps.println("usage: filename support confidence");
    }
    
    public static void listRules(Iterator ruleIterator){
        DecimalFormat format = new DecimalFormat("#0.000");
        while(ruleIterator.hasNext()){
            Rule rule = (Rule) ruleIterator.next();
            System.out.print(rule.toString());
            for(int i=0;i<indexList.size();++i){
                RuleIndex index = (RuleIndex) indexList.get(i);
                System.out.print(", " + 
                        format.format(index.getIndex(rule)));
            }
            System.out.println();                   
        }        
    }
    
    public static void main(String argv[]) throws Exception{
    	double start_m = Runtime.getRuntime().freeMemory();
    	long start_t = System.currentTimeMillis();
        if(argv.length < 3){
            printUsage(System.err);
            System.exit(-1);
        }
        
        double support = Double.parseDouble(argv[1]);
        double confidence = Double.parseDouble(argv[2]);
        
        NameSpace ns = new BasicNameSpace();
        TextInput input = new TextInput(argv[0], ns);
        DirectBuilder output = new DirectBuilder();
        DHPMiner miner = new DHPMiner(input, output, support);
        miner.addProgressListener(new ConsoleMessenger());
        miner.start();
        miner.join();
        RuleSet rs = output.getRuleSet();
        rs.setNameSpace(ns);
        
//        Iterator it = rs.ancensterIterator(new Itemset(new int[0]));
//        while(it.hasNext()){
//            Litemset litemset = (Litemset) it.next();
//            System.out.println(
//                    rs.toString(litemset) + ": " + litemset.count);
//        }

        System.out.println("All Rules: ");
        listRules(rs.getRules(support, confidence));
        double last_m = Runtime.getRuntime().freeMemory();
        long last_t = System.currentTimeMillis();
        System.out.println("Total used memory==>"+(last_m-start_m));
		System.out.println("Total used time==>"+(last_t-start_t));
        
    }
}